package com.ruoyi.imweb.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.controller.BasicController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.im.domain.po.ImRoomPO;
import com.ruoyi.im.domain.po.ImRoomUserPO;
import com.ruoyi.im.domain.qo.ImRoomQO;
import com.ruoyi.im.mapper.ImRoomMapper;
import com.ruoyi.im.service.IImRoomService;
import com.ruoyi.im.service.IImRoomUserService;
import com.ruoyi.imweb.test.entity.test.RoomTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 房间Controller
 * 
 * @author ruoyi
 * @date 2025-10-07
 */
@RestController
@RequestMapping("/room")
public class ImRoomController extends BasicController<ImRoomPO, ImRoomQO, ImRoomMapper, IImRoomService>
{
    @Autowired
    private IImRoomUserService iImRoomUserService;

    /**
     * 导数用
     * @return
     */
    @GetMapping("/test")
    public AjaxResult addTest() {
//        idToNameMap.put("6R0MijpK6M4AIrwaaCY2", "Luke");100
//        idToNameMap.put("SGmFnBZB4xxMv9V4CVlW", "Leia");101
//        idToNameMap.put("6jMsIXUrBHBj7o2cRlau", "Yoda");102
        List<RoomTest> leia = JSONArray.parseArray(CollectionUtil.join(FileUtil.readLines(ResourceUtil.getResource("Leia_rooms.json"), "UTF-8"), ""), RoomTest.class);
        List<RoomTest> luke = JSONArray.parseArray(CollectionUtil.join(FileUtil.readLines(ResourceUtil.getResource("Luke_rooms.json"), "UTF-8"), ""), RoomTest.class);
        List<RoomTest> yoda = JSONArray.parseArray(CollectionUtil.join(FileUtil.readLines(ResourceUtil.getResource("Yoda_rooms.json"), "UTF-8"), ""), RoomTest.class);
        List<RoomTest> ls = new ArrayList<>();
        ls.addAll(leia);
        ls.addAll(luke);
        ls.addAll(yoda);
        int i = 0;
        Map<Integer,ImRoomPO> map = new HashMap<>();
        Map<Integer,List<ImRoomUserPO>> map1 = new HashMap<>();
        for (RoomTest roomTest : ls) {
            i++;
            List<String> users = roomTest.getUsers();
            ImRoomPO imRoomPO = ImRoomPO.builder().roomId(roomTest.getId()).lastUpdated(roomTest.getLastUpdated()).build();
            map.put(i, imRoomPO);
            this.add(imRoomPO);
            if (CollectionUtil.isNotEmpty(users)) {
                List<ImRoomUserPO> imRoomUserPOS = new ArrayList<>();
                for (String user : users) {
                    Long id = null;
                    if (user.equals("6R0MijpK6M4AIrwaaCY2")) {
                        id = 100L;
                    } else if (user.equals("SGmFnBZB4xxMv9V4CVlW")) {
                        id=101L;
                    } else if (user.equals("6jMsIXUrBHBj7o2cRlau")) {
                        id=102L;

                    }else{
                        continue;
                    }
                    ImRoomUserPO build = ImRoomUserPO.builder().roomId(imRoomPO.getId()).unreadCount(0).userId(id).build();
                    imRoomUserPOS.add(build);
//                    iImRoomUserService.insert(build);
                }
                map1.put(i, imRoomUserPOS);
            }
            System.out.println(i);
        }
        for (Map.Entry<Integer, List<ImRoomUserPO>> integerListEntry : map1.entrySet()) {
            ImRoomPO imRoomPO = map.get(integerListEntry.getKey());
            for (ImRoomUserPO imRoomUserPO : integerListEntry.getValue()) {
                imRoomUserPO.setRoomId(imRoomPO.getId());
            }
            iImRoomUserService.batchInsert(integerListEntry.getValue());
        }
//        List<ImRoomPO> collect = map.values().stream().collect(Collectors.toList());
//        this.batchInsert(collect);
        return AjaxResult.success(true);
    }
}
